package org.example.attendancesystem.controller;

import com.google.zxing.WriterException;
import org.example.attendancesystem.utils.QRCodeUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody; // 修复：使用 @RequestBody
import org.springframework.web.bind.annotation.RestController;

import java.io.IOException;
@CrossOrigin
@RestController
public class QRCodeController {

    @Autowired
    private QRCodeUtil qrCodeUtil;

    @PostMapping("/generateQRCode")
    public ResponseEntity<byte[]> generateQRCode(@RequestBody String content) { // 修复：使用 @RequestBody
        try {
            byte[] qrCodeImage = qrCodeUtil.generateQRCode(content);
            return ResponseEntity.ok().body(qrCodeImage);
        } catch (WriterException | IOException e) {
            e.printStackTrace();
            return ResponseEntity.status(500).body(new byte[0]);
        }
    }
}